package com.shop.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.mapper.PhoneMapper;
import com.shop.pojo.Phone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class PhoneServiceImpl implements PhoneService{
    @Autowired
    private PhoneMapper phoneMapper;

    @Override
    public List<Phone> getAll(String name){
        List<Phone> list;
        //如果name没有输入，则查询所以，否则模糊查询
        if(null==name||name.trim().length()==0){
            list=phoneMapper.findAll();
        }else{
            list=phoneMapper.findByNameLike(name);
        }
        return list;
        //List<Phone> phoneList = phoneMapper.findAll();
        //return phoneList;
        //return phoneMapper.findAll();
    }


    @Override
    public PageInfo<Phone> pages(Integer pageNow, Integer pageSize, String name) {
        //1. 设置分页的属性
        Page<Phone> page = PageHelper.startPage(pageNow,pageSize);
        //2. 调用上面的方法
        List<Phone> phoneList = getAll(name);
        //3. 返回的PageInfo对象(封装了分页的一些信息)
        //分页插件的底层涉及到动态代理技术以及拦截器技术 - 面试题
        PageInfo<Phone> pageInfo = new PageInfo<>(phoneList);
        return pageInfo;
    }

}
